/*******************************************************************************
 * Copyright (c) 2011 Jovansonlee B. Cesar.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Jovansonlee B. Cesar - initial API and implementation
 ******************************************************************************/
package com.ivanceras.server.db;

import java.util.Properties;

import com.ivanceras.client.db.DB_Html5;
import com.ivanceras.shared.db.IDatabase;

public class DatabaseManager {

	public static final String POSTGRESQL = "postgresql";
	public static final String BIGTABLE = "bigtable";
	public static final String ORACLE = "oracle";
	public static final String MYSQL = "mysql";
	public static final String SQLITE = "sqlite";
	public static final String SQLDROID = "sqldroid";
	public static final String HTML5 = "html5";

	public static IDatabase create(String dbType, String dbHost, String dbPort, String dbName, String dbUser, String dbPassword, String schema){
		IDatabase db = null;
		
		if(dbType.equals(POSTGRESQL)){
			db = new DB_PostgreSQL(dbHost,dbPort,dbName, dbUser, dbPassword, schema);
		}
		else if(dbType.equals(BIGTABLE)){ /** tentative support for Google BigTable */
			db = new DB_BigTable(schema);
		}
		else if(dbType.equals(ORACLE)){ /** tentative support for Oracle */
			//db = new DB_Oracle(dbHost, dbPort, dbName, dbUser, dbPassword, schema); 
		}
		else if(dbType.equals(MYSQL)){ /** mysql */
			//db = new DB_MySQL(dbHost, dbPort, dbName, dbUser, dbPassword, schema);
		}
		else if(dbType.equals(SQLDROID)){
			db = new DB_SQLDroid(dbName);
		}
		else if(dbType.equals(SQLITE)){
			db = new DB_SQLite(dbName);
		}
		else if(dbType.equals(HTML5)){
			db = new DB_Html5(dbName);
		}
		else{
			try {
				throw new Exception("Database of type "+dbType+" not yet supported!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return db;
	}
	
	public static IDatabase create(Properties prop, Properties customProp)
	{

		
		if(customProp != null){
			//TODO: loop through each of the properties to shorten the code
			String ctmDbType = customProp.getProperty("databaseType");
			String ctmDbHost = customProp.getProperty("databaseHost");
			String ctmDbPort = customProp.getProperty("databasePort");
			String ctmDbName = customProp.getProperty("databaseName");
			String ctmDbUser = customProp.getProperty("databaseUser");
			String ctmDbPassword = customProp.getProperty("databasePassword");
			String ctmDbSchema = customProp.getProperty("defaultSchema");
			
			if(ctmDbType != null && !ctmDbType.trim().equals("")){
				prop.setProperty("databaseType",ctmDbType);
			}
			if(ctmDbHost != null && !ctmDbHost.trim().equals("")){
				prop.setProperty("databaseHost", ctmDbHost);
			}
			if(ctmDbPort != null && !ctmDbPort.trim().equals("")){
				prop.setProperty("databasePort",ctmDbPort);
			}
			if(ctmDbName != null && !ctmDbName.trim().equals("")){
				prop.setProperty("databaseName", ctmDbName);
			}
			if(ctmDbUser != null && !ctmDbUser.trim().equals("")){
				prop.setProperty("databaseUser", ctmDbUser);
			}
			if(ctmDbPassword != null && !ctmDbPassword.trim().equals("")){
				prop.setProperty("databasePassword", ctmDbPassword);
			}
			if(ctmDbSchema != null && !ctmDbSchema.trim().equals("")){
				prop.setProperty("defaultSchema", ctmDbSchema);
			}
		}
		return create(prop);
	}
	public static IDatabase create(Properties prop)
	{
		String dbType = prop.getProperty("databaseType");
		String dbHost = prop.getProperty("databaseHost");
		String dbPort = prop.getProperty("databasePort");
		String dbName = prop.getProperty("databaseName");
		String dbUser = prop.getProperty("databaseUser");
		String dbPassword = prop.getProperty("databasePassword");
		String dbSchema = prop.getProperty("defaultSchema");
		return create(dbType, dbHost, dbPort, dbName, dbUser, dbPassword, dbSchema);
	}
}
